package controller;



import entity.Student;
import service.StudentLoginService;

import service.impl.StudentLoginServiceImpl;

import util.FaceEnginer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Base64;


@WebServlet(urlPatterns = "/studentLoginController")
public class StudentLoginController extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    String op = req.getParameter("op");
    HttpSession session = req.getSession(true);
    StudentLoginService dao = new StudentLoginServiceImpl();
    
    if ("login".equals(op)) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String role = req.getParameter("userType");
        String facePhoto = req.getParameter("facePhoto"); // 可能为null
        
        PrintWriter out = resp.getWriter();
        FaceEnginer f = new FaceEnginer();

        try {
            Student stu = dao.Login(username, password, role);
            if (stu == null) {
                session.setAttribute("file", "账户密码错误");
                resp.sendRedirect("denglu/login.jsp");
                return;
            }

            // 管理员跳过人脸验证
            if ("admin".equals(role)) {
                session.setAttribute("file", "管理员登录成功");  // 添加引号
                session.setAttribute("user", stu);
                resp.sendRedirect("jsp/index.jsp");
                return;
            }

            // 非管理员需要人脸验证
            if (facePhoto == null || facePhoto.isEmpty()) {
                session.setAttribute("file", "请先进行人脸认证");
                resp.sendRedirect("denglu/login.jsp");
                return;
            }

            // 检查用户是否有人脸照片
            if (stu.getFacePhoto() == null || stu.getFacePhoto().isEmpty()) {
                session.setAttribute("file", "该用户未注册人脸信息，请先注册");
                resp.sendRedirect("denglu/login.jsp");
                return;
            }

            // 处理人脸照片
            String base64Image = facePhoto.split(",")[1];
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);
            
            File dir = new File("D:/denglu/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            String imagePath = "D:/denglu/uploaded_image_" + username + ".png";
            File file = new File(imagePath);
            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(imageBytes);
            }

            // 人脸比对
            if (f.imageTo(stu.getFacePhoto(), file.getAbsolutePath()) > 0.8) {
                session.setAttribute("file", "人脸检测通过");
                session.setAttribute("user", stu);
                resp.sendRedirect("jsp/index.jsp");
            } else {
                session.setAttribute("file", "人脸检测未通过");
                resp.sendRedirect("denglu/login.jsp");
            }
            
        } catch (Exception e) {
            session.setAttribute("file", "系统错误: " + e.getMessage());
            resp.sendRedirect("denglu/login.jsp");
        }
    }
}
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
